

<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>怎么样使用Git？ - cxp&#39;s blog</title>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate" />

  
  
  <meta name="description" content=" 什么是Git？
首先一个问题就是：什么是Git？用来..."> 
  
  <meta name="author" content="Alex"> 

  
    <link rel="icon" href="/images/icons/favicon-16x16.png" type="image/png" sizes="16x16">
  
  
    <link rel="icon" href="/images/icons/favicon-32x32.png" type="image/png" sizes="32x32">
  
  
    <link rel="apple-touch-icon" href="/images/icons/apple-touch-icon.png" sizes="180x180">
  
  
    <meta rel="mask-icon" href="/images/icons/stun-logo.svg" color="#333333">
  
  
    <meta rel="msapplication-TileImage" content="/images/icons/favicon-144x144.png">
    <meta rel="msapplication-TileColor" content="#000000">
  

  <link rel="stylesheet" href="/css/style.css">

  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1445822_h1619vhl1nr.css">
  

  
  
  <link rel="stylesheet" href="https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.css">
  

  
  
  <link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css">
  

  <script>
    var CONFIG = window.CONFIG || {};
    var ZHAOO = window.ZHAOO || {};
    CONFIG = {
      isHome: false,
      fancybox: true,
      pjax: false,
      lazyload: {
        enable: true,
        loadingImage: '',
      },
      donate: {
        enable: true,
        alipay: 'https://pic.izhaoo.com/alipay.jpg',
        wechat: 'https://pic.izhaoo.com/wechat.jpg'
      },
      motto: {
        api: '',
        default: '我在开了灯的床头下，想问问自己的心啊。'
      },
      galleries: {
        enable: true
      },
      fab: {
        enable: true,
        alwaysShow: false
      },
      carrier: {
        enable: true
      },
      daovoice: {
        enable: true
      }
    }
  </script>

  

  
<link rel="alternate" href="/atom.xml" title="cxp's blog" type="application/atom+xml">
</head>
<body class="lock-screen">
  <div class="loading"></div>
  


<nav class="navbar">
  <div class="left"></div>
  <div class="center">怎么样使用Git？</div>
  <div class="right">
    <i class="iconfont iconmenu j-navbar-menu"></i>
  </div>
</nav>

  <nav class="menu">
  <div class="menu-wrap">
    <div class="menu-close">
      <i class="iconfont iconbaseline-close-px"></i>
    </div>
    <ul class="menu-content">
      
      
      
      
      <li class="menu-item"><a href="/ " class="underline"> 首页</a></li>
      
      
      
      
      <li class="menu-item"><a href="/galleries " class="underline"> 摄影</a></li>
      
      
      
      
      <li class="menu-item"><a href="/archives " class="underline"> 归档</a></li>
      
      
      
      
      <li class="menu-item"><a href="/tags " class="underline"> 标签</a></li>
      
      
      
      
      <li class="menu-item"><a href="/categories " class="underline"> 分类</a></li>
      
      
      
      
      <li class="menu-item"><a href="/about " class="underline"> 关于</a></li>
      
    </ul>
    <div class="menu-copyright"><p>Powered by <a target="_blank" href="https://hexo.io">Hexo</a>  |  Theme - <a target="_blank" href="https://github.com/izhaoo/hexo-theme-zhaoo">zhaoo</a></p></div>
  </div>
</nav>
  <main id="main">
  <div class="container" id="container">
    <article class="article">
  <div class="wrap">
    <section class="head">
  <img   class="lazyload" data-original="/images/theme/post-image.jpg" src=""  draggable="false">
  <div class="head-mask">
    <h1 class="head-title">怎么样使用Git？</h1>
    <div class="head-info">
      <span class="post-info-item"><i class="iconfont iconcalendar"></i>三月 31, 2019</span
        class="post-info-item">
      
      <span class="post-info-item"><i class="iconfont iconfont-size"></i>2432</span>
    </div>
  </div>
</section>
    <section class="main">
      <section class="content">
        <h1 id="什么是git"><a class="markdownIt-Anchor" href="#什么是git"></a> 什么是Git？</h1>
<p>首先一个问题就是：什么是Git？用来干什么？</p>
<p>维基百科中是这么说的：Git是一种分布式版本控制软件。Git是用C语言开发的，追求高性能，它的TCP监听端口是9418，刚开始是用于Linux内核开发的版本控制，不需要服务器端软件，就可以运作版本控制软件，使得源代码的发布和交流极为方便。</p>
<h1 id="git中的数据结构"><a class="markdownIt-Anchor" href="#git中的数据结构"></a> Git中的数据结构</h1>
<ul>
<li>可变的索引</li>
<li>不可变的、仅追加的对象数据库</li>
</ul>
<p>下面总结下常用的基本语法</p>
<h1 id="基本使用方法"><a class="markdownIt-Anchor" href="#基本使用方法"></a> 基本使用方法</h1>
<p>使用之前需要先安装，附官网地址：<a href="https://www.git-scm.com/download/" target="_blank" rel="noopener" title="Git下载">https://www.git-scm.com/download/</a></p>
<p>使用Git，要知道什么是工作区、暂存区、版本库。<br />
本地进行存储的称之为工作区，然后修改文件之后，先将文件add暂存区，暂存区保存未提交的修改，最后将暂存区的修改commit到版本库（之后还需要push远程）。</p>
<ol>
<li>在windows中安装使用Git，打开Git Bash新建一个空文件夹，用于本地项目的存储，文件夹路径不要使用中文名字。</li>
</ol>
<ul>
<li>mkdir  xxx：创建文件夹</li>
<li>cd xxx：进入文件夹</li>
<li>git init：文件夹初始化为本地库</li>
</ul>
<ol start="2">
<li>修改、添加、提交</li>
</ol>
<ul>
<li>vi <a href="http://README.md" target="_blank" rel="noopener">README.md</a>：新建编辑文档</li>
<li>git add <a href="http://README.md" target="_blank" rel="noopener">README.md</a>：将本地文档添加入暂存区</li>
<li>git commit -m <a href="http://README.md" target="_blank" rel="noopener">README.md</a>：提交到版本分支上</li>
<li>git status：查看暂存区状态</li>
<li>git diff <a href="http://README.md" target="_blank" rel="noopener">README.md</a>：查看修改</li>
</ul>
<ol start="3">
<li>查看日志和回退功能</li>
</ol>
<ul>
<li>git log：查看提交日志</li>
<li>git reflog：记录所有的操作</li>
<li>git reset --hard HEAD^：版本回退当前版本的上一个版本</li>
<li>git reset --hard id：根据版本号进行回退，版本号可以在log中看到</li>
<li>git checkout – <a href="http://README.md" target="_blank" rel="noopener">README.md</a> ：当没有进行add时，可以撤销上一版本add或者提交之后的所有修改</li>
<li>git reset HEAD <a href="http://README.md" target="_blank" rel="noopener">README.md</a>：当这是add之后没有commit，这一步加上一步进行撤销修改</li>
<li>git reset --hard id：当已经commit之后，可以通过回退进行退回到以前版本。</li>
</ul>
<ol start="4">
<li>删除操作和误操作解决办法</li>
</ol>
<ul>
<li>git rm <a href="http://README.md" target="_blank" rel="noopener">README.md</a>：删除文档，并告诉暂存区</li>
<li>git commit -m “remove a file”：提交删除信息</li>
<li>git reset --hard HEAD^：撤销修改</li>
<li>git checkout – <a href="http://READMD.md" target="_blank" rel="noopener">READMD.md</a>：找回删除的文件</li>
</ul>
<ol start="5">
<li>连接、克隆远程库，下面还涉及到连接多个远程库，进行共同管理</li>
</ol>
<ul>
<li>git remote add origin <a href="mailto:git@github.com" target="_blank" rel="noopener">git@github.com</a>:Caixp0612/dragon.git：远程连接github上的远程库</li>
<li>git push -u origin master：将本地库推送到远程</li>
<li>git clone <a href="mailto:git@github.com" target="_blank" rel="noopener">git@github.com</a>:Caixp0612/dragon.git：克隆远程库到本地</li>
</ul>
<ol start="6">
<li>分支管理</li>
</ol>
<ul>
<li>git checkout -b dev：创建并切换分支</li>
<li>git checkout master：切换分支</li>
<li>git branch：查看分支信息</li>
<li>git merge dev：在分支完成修改开发，可以在mater下进行合并</li>
<li>git branch -d dev：当不在分支上进行工作时可以删除分支</li>
<li>git log --graph --pretty=oneline --abbrev-commit：该命令可以看到分支合并情况。</li>
<li>git merge --no-ff -m “merge with no-ff” dev：合并新的分支并禁用fast-forward模式，可以看到分支合并的信息</li>
</ul>
<ol start="7">
<li>保存和恢复工作现场</li>
</ol>
<ul>
<li>git stash：保存当前工作，</li>
<li>git stash list：查看当前保存的工作区</li>
<li>git stash apply stash@{n} 恢复制定的工作区</li>
<li>git stash drop：清空stash内容</li>
<li>git stash pop：恢复同时清空stash内容</li>
<li>git branch -D dev：丢弃未被合并的分支，需要使用参数D</li>
</ul>
<ol start="8">
<li>标签管理，通过打标签使得版本更新更清晰</li>
</ol>
<ul>
<li>git tag v0.1 commit_id：给指定的ID上标签</li>
<li>git show v0.1：查看标签信息</li>
<li>git tag -a v0.1 -m  “xxx”：创建带有说明的标签</li>
<li>git tag -d v0.1：删除标签</li>
<li>git push origin v0.1：将标签推送到远程</li>
<li>git push origin --tags：将全部标签推送到远程</li>
<li>git push origin :refs/tags/v0.9：若已经把标签推送到远程再删除标签的话，先执行本地的删除标签，然后再进行这一步进行远程删除。</li>
</ul>
<ol start="9">
<li>
<p><strong>连接多个远程库进行管理，需要先将本地.ssh文件夹保存的公钥传到远程平台上，使得可以通过ssh通信</strong></p>
<p><code>码云</code></p>
</li>
</ol>
<ul>
<li>git remote add gitee <a href="mailto:git@gitee.com" target="_blank" rel="noopener">git@gitee.com</a>:Caixpeng/dragon.git</li>
<li>git remote -v</li>
<li>git pull gitee master：将远程的库同步到本地，这时候可能会遇到了一个问题就是提示两个库创建历史不一样，这种情况选择忽略，解决方法是在命令后面添加参数 --allow-unrelated–histories，之后就是需要更改合并冲突等一些问题。</li>
<li>git push xxx master：将本地的分支推送到远程，xxx是制定远程库时起的名字</li>
</ul>
<h1 id="待续"><a class="markdownIt-Anchor" href="#待续"></a> 待续</h1>
<pre><code> 之后可以进行定制化Git，学习Git，从使用入手，之后遇到问题时候在进行补充。


 定制化Git可以更改显示颜色，忽略不重要文件，设置别名alias等，设置别名可以在配置文件`.git/config`中进行批量修改。
</code></pre>

      </section>
      <section class="extra">
        
        <ul class="copyright">
  
  <li><strong>本文作者：</strong>Alex</li>
  <li><strong>本文链接：</strong><a href="https://cxpeng.cn/archives/7fd01c5b.html">https://cxpeng.cn/archives/7fd01c5b.html</a></li>
  <li><strong>版权声明：</strong>本博客所有文章均采用<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh"
      rel="external nofollow" target="_blank"> BY-NC-SA </a>许可协议，转载请注明出处！</li>
  
</ul>
        
        
        <section class="donate">
  <div class="qrcode">
    <img   class="lazyload" data-original="https://pic.izhaoo.com/alipay.jpg" src="" >
  </div>
  <div class="icon">
    <a href="javascript:;" target="_blank" rel="noopener" id="alipay"><i class="iconfont iconalipay"></i></a>
    <a href="javascript:;" target="_blank" rel="noopener" id="wechat"><i class="iconfont iconwechat-fill"></i></a>
  </div>
</section>
        
        
  <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/" rel="tag">Git</a></li></ul>

        
<nav class="nav">
  
    <a href="/archives/949db3a1.html"><i class="iconfont iconleft"></i>Python中的多线程</a>
  
  
    <a href="/archives/42250562.html">MySQL学习（一）<i class="iconfont iconright"></i></a>
  
</nav>

      </section>
      
      <section class="comments">
  
  <div class="btn" id="comments-btn">查看评论</div>
  
  
</section>
      
    </section>
  </div>
</article>
  </div>
</main>
  <footer class="footer">
  <div class="footer-social">
    
    
    
    
    
    <a href="tencent://message/?Menu=yes&uin=894519210 " target="_blank" onMouseOver="this.style.color= '#12B7F5'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconQQ "></i>
    </a>
    
    
    
    
    
    <a href="javascript:; " target="_blank" onMouseOver="this.style.color= '#09BB07'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconwechat-fill "></i>
    </a>
    
    
    
    
    
    <a href="https://www.instagram.com/izhaoo/ " target="_blank" onMouseOver="this.style.color= '#DA2E76'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconinstagram "></i>
    </a>
    
    
    
    
    
    <a href="https://github.com/izhaoo " target="_blank" onMouseOver="this.style.color= '#24292E'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  icongithub-fill "></i>
    </a>
    
    
    
    
    
    <a href="mailto:izhaoo@163.com " target="_blank" onMouseOver="this.style.color='#FFBE5B'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconmail"></i>
    </a>
    
  </div>
  <div class="footer-copyright"><p>Powered by <a target="_blank" href="https://hexo.io">Hexo</a>  |  Theme - <a target="_blank" href="https://github.com/izhaoo/hexo-theme-zhaoo">zhaoo</a></p></div>
</footer>
  
      <div class="fab fab-plus">
    <i class="iconfont iconplus"></i>
  </div>
  
  <div class="fab fab-daovoice">
    <i class="iconfont iconcomment"></i>
  </div>
  
  <div class="fab fab-up">
    <i class="iconfont iconcaret-up"></i>
  </div>
  
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</body>

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>




<script src="https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>




<script src="https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.js"></script>




<script src="/js/utils.js"></script>
<script src="/js/modules.js"></script>
<script src="/js/zui.js"></script>
<script src="/js/script.js"></script>




<script>
  (function (i, s, o, g, r, a, m) {
    i["DaoVoiceObject"] = r;
    i[r] = i[r] || function () {
      (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date();
    a = s.createElement(o), m = s.getElementsByTagName(o)[0];
    a.async = 1;
    a.src = g;
    a.charset = "utf-8";
    m.parentNode.insertBefore(a, m)
  })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') +
    "//widget.daovoice.io/widget/0f81ff2f.js", "daovoice")
  daovoice('init', {
    app_id: "abcdefg"
  }, {
    launcher: {
      disableLauncherIcon: true,
    },
  });
  daovoice('update');
</script>



<script>
  (function () {
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
      bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    } else {
      bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
  })();
</script>


<script>
  var _hmt = _hmt || [];
  (function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?4c204d8bc027a0455b5fc642ac334ca8";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>










</html>